/*
 * Created by Huang 嵘才 on 18-2-7 上午11:22
 * Copyright (c) 2018. All rights reserved.
 * Last modified 18-2-6 下午5:00
 */

package io.esirong.image;


import com.bumptech.glide.request.target.BaseTarget;

import androidx.annotation.DrawableRes;
import androidx.annotation.IntRange;

/**
 * 图像加载配置
 * Created by 黄嵘才(HuangRc) on 2018/2/1.
 */

public class ImageLoaderOptions {


    private BaseTarget target = null; //target
    private LoaderResultCallBack loaderResultCallBack;   // 返回图片加载结果
    private ImageSize imageSize;  //设置图片的大小
    private DiskCacheStrategy mDiskCacheStrategy = DiskCacheStrategy.DEFAULT; //磁盘缓存策略
    private int errorDrawable;  //是否展示加载错误的图片
    private boolean asGif = false;   //是否作为gif展示
    private boolean isCrossFade = true; //是否渐变平滑的显示图片,默认为true
    private boolean isSkipMemoryCache = false; //是否跳过内存缓存
    private boolean blurImage = false; //是否使用高斯模糊
    private int holderDrawable;  // 设置展位图
    private int blurValue;   // 高斯模糊参数，越大越模糊
    private int imageRadius = 0;//图片半径
    private boolean isCircle = false;//是不是圆形显示
    private boolean dontAnimate = false;//不播放动画
    private ScaleType scaleType;//缩放模式

    private ImageLoaderOptions(Builder builder) {
        this.asGif = builder.asGif;
        this.errorDrawable = builder.errorDrawable;
        this.holderDrawable = builder.holderDrawable;
        this.imageSize = builder.mImageSize;
        this.isCrossFade = builder.isCrossFade;
        this.isSkipMemoryCache = builder.isSkipMemoryCache;
        this.mDiskCacheStrategy = builder.mDiskCacheStrategy;
        this.blurImage = builder.blurImage;
        this.target = builder.target;
        this.loaderResultCallBack = builder.loaderResultCallBack;
        this.isCircle = builder.isCircle;
        this.blurValue = builder.blurValue;
        this.imageRadius = builder.imageRadius;
        this.scaleType = builder.scaleType;
        this.dontAnimate = builder.dontAnimate;
    }

    public LoaderResultCallBack getLoaderResultCallBack() {
        return loaderResultCallBack;
    }

    public BaseTarget getTarget() {
        return target;
    }


    public int getBlurValue() {
        return blurValue;
    }

    public boolean needImageRadius() {
        return imageRadius > 0;
    }

    public int getImageRadius() {
        return imageRadius;
    }

    public boolean isBlurImage() {
        return blurImage;
    }

    public boolean isCircle() {
        return isCircle;
    }

    public int getHolderDrawable() {
        return holderDrawable;
    }


    public ImageSize getImageSize() {
        return imageSize;
    }


    public int getErrorDrawable() {
        return errorDrawable;
    }


    public boolean isAsGif() {
        return asGif;
    }


    public boolean isCrossFade() {
        return isCrossFade;
    }


    public boolean isSkipMemoryCache() {
        return isSkipMemoryCache;
    }

    public ScaleType getScaleType() {
        return scaleType;
    }

    public boolean isDontAnimate() {
        return dontAnimate;
    }


    public DiskCacheStrategy getDiskCacheStrategy() {
        return mDiskCacheStrategy;
    }


    /**
     * 对应磁盘缓存策略
     */
    public enum DiskCacheStrategy {
        All, NONE, SOURCE, RESULT, DEFAULT
    }

    /**
     * 图片加载的缩放模式
     */
    public enum ScaleType {
        /**
         * 居中
         */
        CENTER_CROP,
        /**
         * Fix
         */
        CENTER_INSIDE,
    }

    public final static class Builder {

        private int holderDrawable = -1;
        private ImageSize mImageSize;
        private int errorDrawable = -1;
        private boolean asGif = false;
        private boolean isCrossFade = false;
        private boolean isSkipMemoryCache = false;
        private boolean blurImage = false;
        private DiskCacheStrategy mDiskCacheStrategy = DiskCacheStrategy.DEFAULT;
        private BaseTarget target = null;
        private LoaderResultCallBack loaderResultCallBack;
        private int blurValue = 15;
        private int imageRadius = 0;
        private boolean isCircle = false;
        private ScaleType scaleType = ScaleType.CENTER_INSIDE;
        private boolean dontAnimate = false;


        public Builder() {
        }

        public Builder placeholder(@DrawableRes int holderDrawable) {
            this.holderDrawable = holderDrawable;
            return this;
        }

        public Builder isCrossFade(boolean isCrossFade) {
            this.isCrossFade = isCrossFade;
            return this;
        }

        public Builder blurImage(boolean blurImage) {
            this.blurImage = blurImage;
            return this;
        }

        public Builder isCircle() {
            this.isCircle = true;
            return this;
        }

        public Builder blurValue(@IntRange(from = 0) int blurvalue) {
            this.blurValue = blurvalue;
            return this;
        }

        public Builder isSkipMemoryCache(boolean isSkipMemoryCache) {
            this.isSkipMemoryCache = isSkipMemoryCache;
            return this;

        }

        public Builder override(int width, int height) {
            this.mImageSize = new ImageSize(width, height);
            return this;
        }

        public Builder asGif(boolean asGif) {
            this.asGif = asGif;
            return this;
        }

        public Builder error(@DrawableRes int errorDrawable) {
            this.errorDrawable = errorDrawable;
            return this;
        }

        public Builder error(LoaderResultCallBack resultCallBack) {
            this.loaderResultCallBack = resultCallBack;
            return this;
        }

        public Builder target(BaseTarget target) {
            this.target = target;
            return this;
        }

        public Builder diskCacheStrategy(DiskCacheStrategy mDiskCacheStrategy) {
            this.mDiskCacheStrategy = mDiskCacheStrategy;
            return this;

        }

        public Builder scale(ScaleType type) {
            this.scaleType = type;
            return this;

        }

        public Builder dontAnimate() {
            this.dontAnimate = true;
            return this;

        }

        public ImageLoaderOptions build() {
            return new ImageLoaderOptions(this);
        }


    }

    /**
     * 对应重写图片size
     */
    public final static class ImageSize {
        private int width = 0;
        private int height = 0;

        public ImageSize(int width, int height) {
            this.width = width;
            this.height = height;
        }

        public int getHeight() {
            return height;
        }

        public int getWidth() {
            return width;
        }
    }
}
